﻿
Imports System.IO
Imports std

Friend Module _sp
    Friend sp As stringsPackage = Nothing

    Private Class strings
        Inherits Global.strings

        Private lang As String = Nothing
        Private WithEvents loc As configMonitor = Nothing

        Public Overrides Function language() As String
            Return lang
        End Function

        Private Function defaultLanguage() As String
            Dim barefilename As String = Nothing
            barefilename = filename(loc.configfile())
            Dim suffix As String = Nothing
            suffix = filenamesuffix(barefilename)
            Return strleft(barefilename, strlen(barefilename) - strlen(suffix) - strlen(filesystem.suffixSeperator))
        End Function

        Private Sub initial()
            assert(Not loc Is Nothing, "loc is not initialized before initial.")
            Dim v As vector(Of String) = Nothing
            If loc.readlist(configNaming.loc.name, emptyString, v) OrElse v Is Nothing OrElse v.size() = 0 Then
                m.clear()
                Dim i As Int64
                For i = 0 To v.size() - 1
                    def(i, v(i))
                Next
            Else
                raiseError("cannot read list from localization configuration file " + lang, _
                           errorHandle.errorType.warning)
            End If
            lang = loc.parameter(configNaming.loc.name, configNaming.loc.language, _
                                 defaultLanguage())
        End Sub

        Public Shared Function createStrings(ByVal langfile As String) As strings
            Dim rtn As strings = Nothing
            rtn = New strings()
            rtn.loc = New configMonitor()
            If Not rtn.loc.open(langfile) Then
                raiseError("cannot open language file " + langfile, errorHandle.errorType.user)
                Return Nothing
            Else
                rtn.initial()
                Return rtn
            End If
        End Function

        Private Sub loc_ConfigChanged() Handles loc.configChanged
            initial()
        End Sub
    End Class

    Public Sub initialStringsPackage(ByVal locpath As String)
        If Not Directory.Exists(locpath) Then
            raiseError("do not have locpath " + locpath + ", ignore initialize stringsPackage.", _
                       errorHandle.errorType.warning)
            Return
        End If

        Dim files() As String = Nothing
        files = Directory.GetFiles(locpath)
        If files Is Nothing OrElse files.Length() = 0 Then
            raiseError("do not have files under " + locpath + ", ignore initialize stringsPackage.", _
                       errorHandle.errorType.warning)
            Return
        End If

        sp.clear()
        Dim i As Int64
        For i = 0 To files.Length() - 1
            Dim ss As strings = Nothing
            ss = strings.createStrings(files(i))
            If Not ss Is Nothing Then
                sp.insertStrings(ss)
            End If
        Next
    End Sub

    Sub New()
        sp = New stringsPackage()
    End Sub
End Module
